<?php

namespace App\Http\Controllers\Admin;

use App\Services\Tools\ObsService;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;                //引用http请求类
use App\Exceptions\BasicException;          //引入框架异常处理类
use App\Response\Response;

class GiftFrameController extends BaseController
{

    /**
     * @api {get} gift/frame/list 礼物边框列表
     * @apiVersion 0.1.0
     * @apiName 礼物边框列表
     * @apiGroup 礼物管理
     *
     * @apiParam {number} page     页数，非必传
     * @apiParam {number} perPage     页数，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id": 32,
     * "name": "抽奖礼物0.1档",
     * "condition1": 50000,
     * "condition2": 200000,
     * "condition3": 1000000,
     * "frame1": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/gold.png",
     * "frame2": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/silver.png",
     * "frame3": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/copper.png",
     * "add_time": 0,
     * "deleted": 0
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": "http://www.na.com/gift/frame?=2",
     * "total": 32
     * }
     * }
     * }
     *
     */
    public function index(Request $request)
    {
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $where = [];
        $giftFrameList = app('repo_gift')->getGiftFrameList($where, $page, $perPage);

        return Response::sendList($giftFrameList, [], [], $code = 200);
    }

    /**
     * @api {post} gift/frame 添加礼物边框
     * @apiVersion 0.1.0
     * @apiName 添加礼物边框
     * @apiGroup 礼物管理
     *
     * @apiParam {string} name     边框名称，必传
     * @apiParam {string} condition1    条件1，非必传
     * @apiParam {string} condition2    条件2，非必传
     * @apiParam {string} condition3    条件3，非必传
     * @apiParam {file} frame1     边框1，非必传
     * @apiParam {file} frame2     边框2，非必传
     * @apiParam {file} frame3     边框3，非必传
     * @apiParam {number} default_score     默认评分，必传
     * @apiParam {number} one_star_score     一星评分，必传
     * @apiParam {number} two_star_score     二星评分，必传
     * @apiParam {number} three_star_score     三星评分，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93020  添加礼物边框失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93032,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function add(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'name' => 'required|max:255',
            'default_score' => 'required|numeric|gt:0|max:2100000000',
            'one_star_score' => 'required|numeric|gt:0|max:2100000000',
            'two_star_score' => 'required|numeric|gt:0|max:2100000000',
            'three_star_score' => 'required|numeric|gt:0|max:2100000000',
        ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $param = $request->only(['name', 'condition1', 'condition2', 'condition3', 'default_score', 'one_star_score', 'two_star_score', 'three_star_score']);
        $frame1 = $request->file('frame1');
        if ($frame1) {
            $res = ObsService::uploadFile($frame1);
            if ($res) {
                $param['frame1'] = $res;
            }
        }

        $frame2 = $request->file('frame2');
        if ($frame2) {
            $res = ObsService::uploadFile($frame2);
            if ($res) {
                $param['frame2'] = $res;
            }
        }

        $frame3 = $request->file('frame3');
        if ($frame3) {
            $res = ObsService::uploadFile($frame3);
            if ($res) {
                $param['frame3'] = $res;
            }
        }

        app('repo_gift')->addGiftFrame($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} gift/frame/update 编辑礼物边框
     * @apiVersion 0.1.0
     * @apiName 编辑礼物边框
     * @apiGroup 礼物管理
     *
     * @apiParam {string} id     边框id，必传
     * @apiParam {string} name     边框名称，非必传
     * @apiParam {string} condition1    条件1，非必传
     * @apiParam {string} condition2    条件2，非必传
     * @apiParam {string} condition3    条件3，非必传
     * @apiParam {file} frame1     边框1，非必传
     * @apiParam {file} frame2     边框2，非必传
     * @apiParam {file} frame3     边框3，非必传
     * @apiParam {number} default_score     默认评分，必传
     * @apiParam {number} one_star_score     一星评分，必传
     * @apiParam {number} two_star_score     二星评分，必传
     * @apiParam {number} three_star_score     三星评分，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93020  编辑礼物边框失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93033,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function update(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'id'   => 'required',
            'name' => 'required|max:255',
            'default_score' => 'required|numeric|gt:0|max:2100000000',
            'one_star_score' => 'required|numeric|gt:0|max:2100000000',
            'two_star_score' => 'required|numeric|gt:0|max:2100000000',
            'three_star_score' => 'required|numeric|gt:0|max:2100000000',
        ]);

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $param = $request->only(['id', 'name', 'condition1', 'condition2', 'condition3', 'default_score', 'one_star_score', 'two_star_score', 'three_star_score']);
        if (app('model_gift_frame')->where('id', $param['id'])->count() <= 0) {
            throw new BasicException(93034);
        }

        $frame1 = $request->file('frame1');
        if ($frame1) {
            $res = ObsService::uploadFile($frame1, []);
            if ($res) {
                $param['frame1'] = $res;
            }
        }

        $frame2 = $request->file('frame2');
        if ($frame2) {
            $res = ObsService::uploadFile($frame2);
            if ($res) {
                $param['frame2'] = $res;
            }
        }

        $frame3 = $request->file('frame3');
        if ($frame3) {
            $res = ObsService::uploadFile($frame3);
            if ($res) {
                $param['frame3'] = $res;
            }
        }

        app('repo_gift')->updateGiftFrame($param);

        return Response::sendData(true);
    }

    /**
     * @api {delete} gift/frame 删除礼物边框
     * @apiVersion 0.1.0
     * @apiName 删除礼物边框
     * @apiGroup 礼物管理
     *
     * @apiParam {string} ids   删除的礼物边框主键（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93021  添加礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93035,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function delete(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $ids = $request->input('ids');
        $arr_ids = explode(',', $ids);
        app('repo_gift')->delGiftFrame($arr_ids);

        return Response::sendData(true);
    }

    /**
     * @api {get} gift/frame 礼物边框详情
     * @apiVersion 0.1.0
     * @apiName 礼物边框详情
     * @apiGroup 礼物管理
     *
     * @apiParam {string} id   礼物边框id，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "id": 1,
     * "name": "tets",
     * "condition1": 1,
     * "condition2": 11,
     * "condition3": 111,
     * "frame1": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/gold.png",
     * "frame2": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/silver.png",
     * "frame3": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/copper.png",
     * "add_time": 0,
     * "deleted": 0
     * }
     * }
     *
     */

    public function show(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }

        $id = $request->input('id');
        $detail = app('repo_gift')->giftFrameDetail($id);

        return Response::sendData($detail);
    }
}
